package com.saianfu.flowable.demo.controller.result

import io.swagger.annotations.ApiModel
import java.io.Serializable

/**
 * 统一API响应结果封装
 *
 */
@ApiModel
class ResponseData<T> : Serializable {
    /**
     * 响应状态码
     */
    var code: Int? = null

    /**
     * 响应信息
     */
    var message: String? = null

    /**
     * 响应对象
     */
    var data: T? = null
        private set

    private constructor() {}
    private constructor(data: T) {
        this.data = data
    }

    private constructor(message: String) {
        this.message = message
    }

    private constructor(code: Int, message: String) {
        this.code = code
        this.message = message
    }

    private constructor(code: Int, message: String?, data: T) {
        this.code = code
        this.message = message
        this.data = data
    }

    fun setData(data: T) {
        this.data = data
    }

    companion object {
        fun success(): ResponseData<String?> {
            return ResponseData(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.message, null)
        }

        fun <E> success(`object`: E): ResponseData<E> {
            return ResponseData<E>(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.message, `object`)
        }

        fun <E> success(code: Int, message: String, `object`: E): ResponseData<E> {
            return ResponseData<E>(code, message, `object`)
        }

        fun error(): ResponseData<*> {
            return ResponseData<Any?>(ResponseCode.FAILED.code, ResponseCode.FAILED.message, null)
        }

        fun error(code: ResponseCode): ResponseData<*> {
            return ResponseData<Any?>(code.code, code.message, null)
        }

        fun error(message: String?): ResponseData<*> {
            return ResponseData<Any?>(ResponseCode.FAILED.code, message, null)
        }

        fun error(code: Int, message: String?): ResponseData<*> {
            return ResponseData<Any?>(code, message, null)
        }

        fun <E> error(code: Int, message: String, `object`: E): ResponseData<E> {
            return ResponseData<E>(code, message, `object`)
        }
    }
}